﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_SmartParking_Models;

namespace WpfApp_SmartParking.ViewModels.Pages;

public class ViewModelBase: BindableBase, INavigationAware
{
    public string? PageTitle { get; set; }
    public bool IsCanClose { get; set; }
    public string? SearchKey { get; set; }

    IRegionManager _regionManager;

    private string? _navitionViewName { get; set; }

    public DelegateCommand? CloseViewCommand { get; set; }

    public DelegateCommand<string>? RefreshMenuCommand { get; set; }

    public DelegateCommand<Object?>? OpenMenuDetailWindowCommand { get; set; }

    public DelegateCommand<Object?>? DeleteMenuCommand { get; set; }

    public ViewModelBase(IRegionManager regionManager)
    {
        _regionManager = regionManager;

        CloseViewCommand = new DelegateCommand(DoCloseView);
    }

    private void DoCloseView()
    {
        var _regionViews = _regionManager.Regions["MainTabRegion"];
        var _regionView = _regionViews.Views.FirstOrDefault(it => it.GetType().Name == _navitionViewName);
        if (_regionView != null)
        {
            _regionViews.Remove(_regionView);
        }
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        _navitionViewName = navigationContext.Uri.ToString();
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
        
    }
}
